<?php

namespace App\Http\Controllers\Base;

use App\Http\Controllers\Controller;
use App\Http\Requests\Blog\BlogRequest;
use Illuminate\Http\Request;
use App\Handlers\FileUploadHandler;


class FileController extends Controller
{
    /**
     * 上传文件
     * @param Request $request
     * @param ImageUploadHandler $uploader
     * @return mixed
     */
    public function upload(Request $request, FileUploadHandler $uploader) {
        if ($request->file) {
            $folder = $request->folder ?: 'default';
            $filePrefix = $request->filePrefix ?: $folder;
            $result = $uploader->save($request->file, $folder, $filePrefix);
            if ($result) {
                return $this->success('上传成功', $result['path']);
            }

            return $this->unValidate('文件后缀只能为"png", "jpg", "gif", "jpeg"');
        }

        return $this->unValidate('为查询到文件');
    }
}
